package com.danskin.wechat.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.dao.ActivityApplyDao;
import com.danskin.wechat.dao.CustActivityApplyDao;
import com.danskin.wechat.dao.CustInfoExDao;
import com.danskin.wechat.entity.ActivityApply;
import com.danskin.wechat.entity.CustActivityApply;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.example.ActivityApplyExample;
import com.danskin.wechat.entity.example.CustActivityApplyExample;
import com.danskin.wechat.entity.example.CustActivityApplyExample.Criteria;
import com.danskin.wechat.service.CustActivityApplyService;
import com.danskin.wechat.util.Page;
@Service
public class CustActivityApplyServiceImpl implements CustActivityApplyService{
	@Autowired
    private CustActivityApplyDao custActivityApplyDao;
	@Autowired
	private CustInfoExDao  custInfoExDao;
	
	@Autowired
	private ActivityApplyDao activityApplyDao;
	

	@Override
	@Transactional
	public boolean insertCustActivityApply(String activityId, String openId, JSONArray dataLsit) {
		CustInfoEx ex = custInfoExDao.queryCustInfoExById(openId);
		/*List<CustActivityApply> list = custActivityApplyDao.findCustActivityApplyValidate();*/
		boolean flag = true;
		if(ex!=null){
			List<Integer> listId = custActivityApplyDao.queryCustActivityApply(ex.getId(),activityId);
			if(listId.size()>0){
				flag = false;
			}else{
				if(dataLsit!=null){
					for(int j=0;j<dataLsit.size();j++){
						CustActivityApply custActivityApply = new CustActivityApply();
						JSONObject job =  (JSONObject) dataLsit.get(j);
					    String code = job.get("code").toString();
					    String value = job.get("value").toString();
					    /*custActivityApply.setCustExId(ex.getId());*/
					    custActivityApply.setActivityId(Integer.parseInt(activityId));
					    custActivityApply.setApplyCode(code);
					    custActivityApply.setApplyValue(value);
					    custActivityApplyDao.insertCustActivityApply(custActivityApply);
					}
				}
			}	
		}	
		return flag;
	}



	@Override
	public List<Integer> queryCustActivityApplyController(Integer custExId, String activityId) {
		// TODO Auto-generated method stub
		return custActivityApplyDao.queryCustActivityApply(custExId,activityId);
	}
	public List<CustActivityApply> findCustActivityApplyId(Integer id) {
		// TODO Auto-generated method stub
		return custActivityApplyDao.findCustActivityApplyId(id);
	}
	
	
	//根据活动id查询   报名该活动下报名人数
	@Override
	public Map<String, Integer> selectActivityCountByActivityId(Map<String, Object> map) {
		Map<String, Integer> map2 = custActivityApplyDao.selectActivityCountByActivityId(map);
		return map2;
	}


	//根据openId和活动id   判断是否参加此活动
	@Override
	public int selectApplyCountByActivityIdAndOpenId(Integer id, String openId) {
		CustActivityApplyExample example=new CustActivityApplyExample();
		Criteria criteria = example.createCriteria();
		criteria.andActivityIdEqualTo(id);
		criteria.andOpenIdEqualTo(openId);
		int i = custActivityApplyDao.countByExample(example);
		return i;
	}



	@Override
	public List<Map<String, Object>> listPageMyActivityListByOpenId(Page page) {
		List<Map<String,Object>> list = custActivityApplyDao.listPageMyActivityListByOpenId(page);
		return list;
	}



	@Override
	public List<Map<String, Object>> selectMyActivityListByOpenId(Map<String, Object> map) {
		List<Map<String,Object>> list = custActivityApplyDao.selectMyActivityListByOpenId(map);
		return list;
	}



	@Override
	public List<ActivityApply> selectActivityApplyOptionByActivityId(Integer id) {
		ActivityApplyExample example=new ActivityApplyExample();
		com.danskin.wechat.entity.example.ActivityApplyExample.Criteria criteria = example.createCriteria();
		criteria.andActivityIdEqualTo(id);
		List<ActivityApply> list = activityApplyDao.selectByExample(example);
		return list;
	}



	@Override
	public int saveActivityApplySelective(CustActivityApply apply) {
		int i = custActivityApplyDao.insertSelective(apply);
		return i;
	}



	@Override
	public List<Map<String, Object>> listPageActivityApplyStatic(Page page) {
		List<Map<String,Object>> list = custActivityApplyDao.listPageActivityApplyStatic(page);
		return list;
	}



	@Override
	public List<CustActivityApply> selectCustActivityApplyOptionByActivityId(Integer id,String openId) {
		CustActivityApplyExample example=new CustActivityApplyExample();
		Criteria criteria = example.createCriteria();
		criteria.andActivityIdEqualTo(id);
		criteria.andOpenIdEqualTo(openId);
		List<CustActivityApply> list = custActivityApplyDao.selectByExample(example);
		
		
		return list;
	}



	@Override
	public int deleteCustActivityApplyByActivityId(Integer id) {
		CustActivityApplyExample example=new CustActivityApplyExample();
		Criteria criteria = example.createCriteria();
		criteria.andActivityIdEqualTo(id);
		int i = custActivityApplyDao.deleteByExample(example);
		return i;
	}
}
